using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Media_Renamer {
    public partial class ImageSlide : UserControl {
        private int actualIndex =0;
        private List<Image> _images;
        public delegate void ImageDroppedDelegate(Image image);
        public event ImageDroppedDelegate ImageDropped;


        public ImageSlide() {
            InitializeComponent();
            _images = new List<Image>();
        }
        

        public List<Image> getImages() {
            return _images;
        }
        public void addImage(Image image) {
            _images.Add(image);
            actualIndex = _images.Count - 1;
            SlideTo(actualIndex);
            refreshButtons();
        }
        public void addImage(List<Image> images) {
            addImage(images, true);
        }
        public void addImage(List<Image> images, bool clear) {
            if (clear) {
                _images = images;
                actualIndex = 0;
            } else {
                _images.AddRange(images);
                actualIndex = _images.Count-1;
            }


            RefreshSlider();
            refreshButtons();
        }
        public void Delete() {
            Delete(actualIndex);
        }
        public void Delete(int index) {
            if (_images.Count - 1 >= index) {
                _images.RemoveAt(index);
                if (_images.Count - 1 >= index) {
                    SlideTo(index);
                } else if (_images.Count > 0) {
                    SlideTo(index - 1);
                } else {
                    Clear();
                }
                
            }
        }

        public void Clear() {
            this.picHolder.Image = null;
            actualIndex = 0;
            this.lblNavigation.Text = actualIndex + "/" + _images.Count;
            _images.Clear();
            refreshButtons();
        }
        public void SlideTo(int index) {
            this.picHolder.Image = _images[index];
            actualIndex = index;
            this.lblNavigation.Text = actualIndex + 1 + "/" + _images.Count;
            refreshButtons();

        }
        private void refreshButtons() {
            this.btnPrevious.Visible = (actualIndex > 0);
            this.btnNext.Visible = (actualIndex < _images.Count - 1);
        }
        public void FreeImage() {
            this.picHolder.Image = null;
        }
        public void RefreshSlider() {
            if (_images.Count > 0) {
                SlideTo(actualIndex);
            } else {
                Clear();
            }
        }
        private void ImageSlide_DragDrop(object sender, DragEventArgs e) {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (string file in files) {
                Image newImage = Image.FromFile(file);
                newImage.Tag = file;
                _images.Add(newImage);
                SlideTo(_images.Count - 1);
                ImageDropped(newImage);
            }
        }

        private void ImageSlide_DragEnter(object sender, DragEventArgs e) {
            e.Effect = DragDropEffects.Copy;
            Console.Out.Write("Enter");
        }

        private void btnNext_Click(object sender, EventArgs e) {
            if (actualIndex >= _images.Count) {
                SlideTo(0);
            } else {
                SlideTo(actualIndex + 1);
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e) {
            if (actualIndex >0) {
                SlideTo(actualIndex - 1);
            } else {
                SlideTo(_images.Count-1);
            }
        }
	
    }
    public class ImageSlideArgs {
    }
}
